package com.k.liuzh.extraffic.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.k.liuzh.extraffic.Database.ChongZhiBean;
import com.k.liuzh.extraffic.Database.ChongZhiDao;
import com.k.liuzh.extraffic.R;

import java.util.List;

public class ZhangDan_Fragment extends Fragment {
    private static final String TAG = "ZhangDan_Fragment";
    private ListView listView;
    private Button button;
    private Spinner spinner;
    public List<ChongZhiBean> list;

    @Nullable

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zhangdan, container, false);

        listView = view.findViewById(R.id.listView_zhangdan);
        spinner = view.findViewById(R.id.spn_zhangdan_updown);
        button = view.findViewById(R.id.btn_zhangdan_chaxun);
        list = new ChongZhiDao(getContext()).queryByTimeDown();
        listView.setAdapter(new MyAdapter(getContext(), list));

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String upOrdown = spinner.getSelectedItem().toString();
                if("时间升序".equals(upOrdown)){
                    list = new ChongZhiDao(getContext()).queryByTimeUp();
                    listView.setAdapter(new MyAdapter(getContext(), list));
                }else if("时间降序".equals(upOrdown)){
                    list = new ChongZhiDao(getContext()).queryByTimeDown();
                    listView.setAdapter(new MyAdapter(getContext(), list));
                }
            }
        });
        return view;
    }

    public class MyAdapter extends BaseAdapter {

        private Context context;
        private List<ChongZhiBean> list;

        public MyAdapter(Context context, List<ChongZhiBean> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHoldr viewHoldr = null;
            LayoutInflater inflater = LayoutInflater.from(context);
            if( viewHoldr == null){
                viewHoldr = new ViewHoldr();
                view = inflater.inflate(R.layout.listview_zhangdan,null);
                viewHoldr.mXuhao = view.findViewById(R.id.textview_listview_zhangdan_xuhao);
                viewHoldr.mChehao = view.findViewById(R.id.textview_listview_zhangdan_chehao);
                viewHoldr.mJine = view.findViewById(R.id.textview_listview_zhangdan_jine);
                viewHoldr.mUsername = view.findViewById(R.id.textview_listview_zhangdan_username);
                viewHoldr.mTime = view.findViewById(R.id.textview_listview_zhangdan_shijian);
                view.setTag(viewHoldr);
            }else{
                viewHoldr = (ViewHoldr) view.getTag();
            }
            ChongZhiBean bean = list.get(i);
            viewHoldr.mXuhao.setText(i+1+"");
            viewHoldr.mChehao.setText(bean.getCarId()+"");
            viewHoldr.mJine.setText(bean.getJinE()+"");
            viewHoldr.mUsername.setText(bean.getUserName());
            viewHoldr.mTime.setText(bean.getTime().toString());
            return view;
        }

        class ViewHoldr {
            TextView mXuhao;
            TextView mChehao;
            TextView mJine;
            TextView mUsername;
            TextView mTime;
        }
    }
}
